package com.mhstay.hook;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.liferay.portal.kernel.events.Action;
import com.liferay.portal.kernel.events.ActionException;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.struts.LastPath;
import com.liferay.portal.kernel.util.PrefsPropsUtil;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.User;
import com.liferay.portal.util.PortalUtil;

public class CustomLandingAction extends Action {
	/* (non-Java-doc)
	 * @see com.liferay.portal.kernel.events.Action#Action()
	 */
	public CustomLandingAction() {
		super();
	}

	/* (non-Java-doc)
	 * @see com.liferay.portal.kernel.events.Action#run(HttpServletRequest arg0, HttpServletResponse arg1)
	 */
	public void run(HttpServletRequest request, HttpServletResponse response) throws ActionException {

		// line inserted by mPower Labs
		String path = getLandingPath(request);

		if (Validator.isNotNull(path)) {
			LastPath lastPath = new LastPath(
				StringPool.BLANK, path, new HashMap<String, String[]>());

			HttpSession session = request.getSession();

			session.setAttribute(WebKeys.LAST_PATH, lastPath);
		}		
	}
	
	private String getLandingPath(HttpServletRequest request) {

		long companyId = PortalUtil.getCompanyId(request);
		
		String path = null;

		User user = null;
		try {
			user = PortalUtil.getUser(request);
		} catch (PortalException e) {
			e.printStackTrace();
		} catch (SystemException e) {
			e.printStackTrace();
		}
		
		if (Validator.isNotNull(user)) {
			String forward = user.getOpenId();
			
			if (Validator.isNotNull(forward)) {
				path = StringPool.SLASH + forward;
			} else {
				path = StringPool.SLASH + "user" + StringPool.SLASH + user.getScreenName();
			}
		}
		
		// if path is still null
		if (Validator.isNull(path)) {
			try {
				path = PrefsPropsUtil.getString(
					companyId, PropsKeys.DEFAULT_LANDING_PAGE_PATH);
			} catch (SystemException e) {
				e.printStackTrace();
			}			
		}
		
		return path;
	}
}